//@dart=2.12
import 'dart:convert';

import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/bad_event/bad_event_api.dart';
import 'package:ls_work/bad_event/response/bad_event_list_data.dart';
import 'package:ls_work/bad_event/response/event_detail_data.dart';
import 'package:ls_work/bad_event/response/event_equipment_list_data.dart';
import 'package:ls_work/net/post_request.dart';

class BadEventModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  Future<List<BadEventListData>> badEventList(
      {String? elderNameLike,
      dynamic eventType,
      String? createDateStart,
      String? createDateEnd,
      int page = 1,
      int rows = 20,
      dynamic archivingStatus}) async {
    var map = {
      "elderNameLike": elderNameLike,
      "eventType": eventType,
      "createDateStart": createDateStart,
      "createDateEnd": createDateEnd,
      "page": page,
      "rows": rows,
      "archivingStatus": archivingStatus,
    };
    map.removeWhere((key, value) =>
        value is String ? value.isNullOrEmpty() : value == null);
    var data = await HttpGo.instance.postData(BadEventApi.eventList,
        data: map,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?) ?? [])
          .map((e) => BadEventListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///事件详情
  Future<EventDetailData> eventDetail({dynamic id, bool wrapper = true}) async {
    var map = {"id": id, "wrapper": wrapper};
    var data = await HttpGo.instance.postData(BadEventApi.eventDetail,
        data: map,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return EventDetailData.fromJson(data['data']);
    } else {
      throw data['msg'];
    }
  }

  ///设备列表
  Future<List<EventEquipmentListData>> eventEquipment() async {
    var data = await HttpGo.instance.postData(
      BadEventApi.equipmentList,
    );
    if ("0" == data['code']) {
      return ((data['data'] as List?) ?? [])
          .map((e) => EventEquipmentListData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  ///服务组列表
  Future<List> serviceGroupList() async {
    var data = await HttpGo.instance.postData(BadEventApi.serviceGroupList);
    if ("0" == data['code']) {
      return (data['data']['rows'] as List?) ?? [];
    } else {
      throw data['msg'];
    }
  }

  ///创建不良事件
  Future<Map> createBadEvent(
      Map<String, dynamic> map, dynamic eventType) async {
    var url = eventType == 1
        ? BadEventApi.createBadEvent
        : eventType == 5
            ? BadEventApi.createOtherBadEvent
            : BadEventApi.createMedicineBadEvent;
    var submitMap = map;
    // if(eventType==4){
    //   submitMap = {
    //     "jsonStr":map['jsonStr']
    //   };
    // }
    var data = await HttpGo.instance.postData(url,
        data: submitMap,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///创建交班
  Future<Map> createHandOver(
    Map<String, dynamic> map,
  ) async {
    var data = await HttpGo.instance.postData(BadEventApi.createHandover,
        data: map, options: Options(contentType: Headers.jsonContentType));
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///更新事件
  Future<Map> updateEvent(Map<String, dynamic> map, dynamic eventType) async {
    var url = eventType == 1
        ? BadEventApi.updateBadEvent
        : eventType == 5
            ? BadEventApi.updateOtherBadEvent
            : BadEventApi.updateMedicineBadEvent;
    var data = await HttpGo.instance.postData(url,
        data: map,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///事件归档
  Future<Map> eventArchiving(
      {dynamic id,
      int archivingStatus = 1,
      dynamic controllable,
      dynamic approveGrade}) async {
    var map = {
      "id": id,
      "archivingStatus": archivingStatus,
      "controllable": controllable,
      "approveGrade": approveGrade,
    };

    map['jsonStr'] = jsonEncode(map);
    var data = await HttpGo.instance.postData(BadEventApi.eventArchiving,
        data: map,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ('0' == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  Future<Map> eventCancel({dynamic id, int archivingStatus = 2}) async {
    var map = {"id": id, "archivingStatus": archivingStatus};
    map['jsonStr'] = jsonEncode(map);
    var data = await HttpGo.instance.postData(BadEventApi.eventArchiving,
        data: map,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ('0' == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }
}
